/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlador;

import controlador.exceptions.IllegalOrphanException;
import controlador.exceptions.NonexistentEntityException;
import controlador.exceptions.PreexistingEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.transaction.UserTransaction;
import modelo.Administradore;
import modelo.Usuarios;
import java.util.ArrayList;

/**
 *
 * @author eltorre
 */
public class AdministradoreJpaController implements Serializable {

    public AdministradoreJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Administradore administradore) throws IllegalOrphanException, PreexistingEntityException, Exception {
        List<String> illegalOrphanMessages = null;
        Usuarios usuariosOrphanCheck = administradore.getUsuarios();
        if (usuariosOrphanCheck != null) {
            Administradore oldAdministradoreOfUsuarios = usuariosOrphanCheck.getAdministradore();
            if (oldAdministradoreOfUsuarios != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("The Usuarios " + usuariosOrphanCheck + " already has an item of type Administradore whose usuarios column cannot be null. Please make another selection for the usuarios field.");
            }
        }
        if (illegalOrphanMessages != null) {
            throw new IllegalOrphanException(illegalOrphanMessages);
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Usuarios usuarios = administradore.getUsuarios();
            if (usuarios != null) {
                usuarios = em.getReference(usuarios.getClass(), usuarios.getId());
                administradore.setUsuarios(usuarios);
            }
            em.persist(administradore);
            if (usuarios != null) {
                usuarios.setAdministradore(administradore);
                usuarios = em.merge(usuarios);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findAdministradore(administradore.getId()) != null) {
                throw new PreexistingEntityException("Administradore " + administradore + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Administradore administradore) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Administradore persistentAdministradore = em.find(Administradore.class, administradore.getId());
            Usuarios usuariosOld = persistentAdministradore.getUsuarios();
            Usuarios usuariosNew = administradore.getUsuarios();
            List<String> illegalOrphanMessages = null;
            if (usuariosNew != null && !usuariosNew.equals(usuariosOld)) {
                Administradore oldAdministradoreOfUsuarios = usuariosNew.getAdministradore();
                if (oldAdministradoreOfUsuarios != null) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("The Usuarios " + usuariosNew + " already has an item of type Administradore whose usuarios column cannot be null. Please make another selection for the usuarios field.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (usuariosNew != null) {
                usuariosNew = em.getReference(usuariosNew.getClass(), usuariosNew.getId());
                administradore.setUsuarios(usuariosNew);
            }
            administradore = em.merge(administradore);
            if (usuariosOld != null && !usuariosOld.equals(usuariosNew)) {
                usuariosOld.setAdministradore(null);
                usuariosOld = em.merge(usuariosOld);
            }
            if (usuariosNew != null && !usuariosNew.equals(usuariosOld)) {
                usuariosNew.setAdministradore(administradore);
                usuariosNew = em.merge(usuariosNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = administradore.getId();
                if (findAdministradore(id) == null) {
                    throw new NonexistentEntityException("The administradore with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Administradore administradore;
            try {
                administradore = em.getReference(Administradore.class, id);
                administradore.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The administradore with id " + id + " no longer exists.", enfe);
            }
            Usuarios usuarios = administradore.getUsuarios();
            if (usuarios != null) {
                usuarios.setAdministradore(null);
                usuarios = em.merge(usuarios);
            }
            em.remove(administradore);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Administradore> findAdministradoreEntities() {
        return findAdministradoreEntities(true, -1, -1);
    }

    public List<Administradore> findAdministradoreEntities(int maxResults, int firstResult) {
        return findAdministradoreEntities(false, maxResults, firstResult);
    }

    private List<Administradore> findAdministradoreEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Administradore as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Administradore findAdministradore(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Administradore.class, id);
        } finally {
            em.close();
        }
    }

    public int getAdministradoreCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Administradore as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
